﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Net;
using EtlComm;
using System.Xml.Linq;

namespace EtlContract
{
    [ServiceContract(Namespace = "http://sungoth.com")]
    public interface IEtlServices
    {
        /// <summary>
        /// 获取多播地址
        /// </summary>
        /// <returns>多播地址</returns>
        [OperationContract]
        string GetMuitlCastAddress();
        /// <summary>
        /// 取ETL服务启动时间
        /// </summary>
        /// <returns>启动时间</returns>
        [OperationContract]
        DateTime GetStartTime();
        /// <summary>
        /// 取ETL的公共Key
        /// </summary>
        /// <returns>publickey</returns>
        [OperationContract]
        string GetPublicKey();
        /// <summary>
        /// 获取加油站任务信息
        /// </summary>
        /// <returns>加油站列表xml字符串</returns>
        [OperationContract]
        string GetStationTaskRunInfo();
        /// <summary>
        /// 获取任务信息
        /// </summary>
        /// <returns>任务信息xml字符串</returns>
        [OperationContract]
        string GetTaskInfo();
        /// <summary>
        /// 停止抽取数据
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        bool Stop();
        /// <summary>
        /// 暂停抽取数据
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        bool Pause();
        /// <summary>
        /// 继续抽取数据
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        bool Continue();
        /// <summary>
        /// 重新加载配置
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        bool ReLoadConfig();
    }

    public class WCFEtlServices : IEtlServices
    {

        #region IEtlServices 成员

        public string GetMuitlCastAddress()
        {
            IPAddress ipa = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();
            byte[] ipArray = ipa.GetAddressBytes();
            return string.Format(EtlGlobal.MuilCastAddress, ipArray[3]);
        }

        public DateTime GetStartTime()
        {
            return EtlGlobal.EtlStartTime;
        }

        public string GetPublicKey()
        {
            return EtlGlobal.Public_Key;
        }

        public string GetStationTaskRunInfo()
        {
            XDocument x = XDocument.Load(EtlGlobal.XmlStationFile);
            return x.ToString();
        }

        public string GetTaskInfo()
        {
            XDocument x = XDocument.Load(EtlGlobal.XmlTaskFile);
            return x.ToString();
        }

        public bool Stop()
        {
            throw new NotImplementedException();
        }

        public bool Pause()
        {
            throw new NotImplementedException();
        }

        public bool Continue()
        {
            throw new NotImplementedException();
        }

        public bool ReLoadConfig()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
